########
# Usage: Rscript detail_cell_boxplot2.R 'Adipose Tissue' "wilcox.test" "Healthy,HCC"
# 传入参数： target = 'Adipose Tissue'  
#            testMethod = "wilcox.test"      # testMethod = "wilcox.test"   or   testMethod = "t.test"
#            cohort.selected = c('Healthy', 'HCC')
########
library(ggplot2)
library(ggpubr)
library(RColorBrewer)

library(optparse)

option_list <- list(
  make_option("--t", default = "", type = "character", help = "target"),
  make_option("--absInput", default = "", type = "character", help = "abs input file"),
  make_option("--relInput", default = "", type = "character", help = "rel input file"),
  make_option("--l", default = "", type = "character", help = "log"),
  make_option("--m", default = "", type = "character", help = "test method"),
  make_option("--c", default = "", type = "character", help = "cohort selected"),
  make_option("--absOut", default = "", type = "character", help = "abs output png file"),
  make_option("--relOut", default = "", type = "character", help = "rel output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

Args = commandArgs()
target = opt$t
testMethod = opt$m
cohort.selected = opt$c
cohort.selected = as.vector(unlist(strsplit(cohort.selected, ',')))

# target = 'Adipose Tissue'
# testMethod = "wilcox.test"
# cohort.selected = c('Healthy', 'HCC')
# path = "D:\\exo\\905\\submit 7\\detail_cell"
# setwd(path)


targetType = 'cell'
w=6
h=6

infile =  opt$absInput
outfile =  opt$absOut
data.target = read.csv(infile, sep=',', header = 1, row.names = NULL)
colnames(data.target) = c('sample', 'type', 'cohort', 'sampleSize', 'score')
data.target = data.target[data.target$cohort %in% cohort.selected, ]
data.target$label = paste(data.target$cohort, '\n', '(', data.target$sampleSize,')', sep='')   ### label

cols = c(brewer.pal(9,"Pastel1"), brewer.pal(8,"Set3"))
pdf(outfile, width=w, height=h)
my_comparisons <- list(unique(data.target$label))
p = ggboxplot(data.target, x="label",y="score", 
              color="#5C5C5C", fill='label', width = 0.6, palette=cols,
              bxp.errorbar = T, bxp.errorbar.width = 0.3, size = 0.8,
              add = "jitter",add.params =list(size=0.2, color='#5C5C5C'),
              outlier.shape= NA)+
  stat_compare_means(comparisons = my_comparisons, method=testMethod,label = "p.signif", vjust = 0.5)+
  ylab('Absolute abundance')+xlab('')+
  ggtitle(target)+
  theme_bw() + theme(panel.grid = element_blank())+
  theme(axis.title = element_text(size = 12, face = "bold"))+
  theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))+
  guides(fill = FALSE, colour = FALSE)+
  theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 360, hjust = 0.5),
        axis.title = element_text(size = 14, face = "bold"))
print(p)
dev.off()


infile =  opt$relInput
outfile =  opt$relOut
data.target = read.csv(infile, sep=',', header = 1, row.names = NULL)
colnames(data.target) = c('sample', 'type', 'cohort', 'sampleSize', 'score')
data.target = data.target[data.target$cohort %in% cohort.selected, ]
data.target$label = paste(data.target$cohort, '\n', '(', data.target$sampleSize,')', sep='')   ### label

cols = c(brewer.pal(9,"Pastel1"), brewer.pal(8,"Set3"))
pdf(outfile, width=w, height=h)
my_comparisons <- list(unique(data.target$label))
p = ggboxplot(data.target, x="label",y="score", 
              color="#5C5C5C", fill='label', width = 0.6, palette=cols,
              bxp.errorbar = T, bxp.errorbar.width = 0.3, size = 0.8,
              add = "jitter",add.params =list(size=0.2, color='#5C5C5C'),
              outlier.shape= NA)+
  stat_compare_means(comparisons = my_comparisons, method=testMethod,label = "p.signif", vjust = 0.5)+
  ylab('Absolute abundance')+xlab('')+
  ggtitle(target)+
  theme_bw() + theme(panel.grid = element_blank())+
  theme(axis.title = element_text(size = 12, face = "bold"))+
  theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5))+
  guides(fill = FALSE, colour = FALSE)+
  theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA),
        axis.text.y = element_text(face = 'bold',color = 'black',size = 10, angle = 360),
        axis.text.x = element_text(face = 'bold',color = 'black',size = 10, angle = 360, hjust = 0.5),
        axis.title = element_text(size = 14, face = "bold"))
print(p)
dev.off()